package com.tjsinfo.tjsoa.workplan.dao;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.dao.DataAccessResourceFailureException;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.tjsinfo.tjsoa.workplan.vo.WorkPlanExamine;
import com.tjsinfo.tjsoa.workplan.vo.WorkPlanMain;

public class WorkPlanMainDao extends HibernateBaseDao implements IWorkPlanMainDao {
	
	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.workplan.hibernatedao.IWorkPlanMainDao#selectOneById(java.lang.Integer)
	 */
	public WorkPlanMain selectOneById(String id) {
		// TODO Auto-generated method stub
		return (WorkPlanMain) getHibernateTemplate().get(WorkPlanMain.class, id);
	}

	/**
	 * 根据计划ID
	 */
	/*public WorkPlanMain selectWorkPlan(final String jihua)
	{

		return (WorkPlanMain)this.getHibernateTemplate().execute(
				new HibernateCallback()
				{

					public Object doInHibernate(Session arg0)
							throws HibernateException, SQLException {
						// TODO Auto-generated method stub
						Query query =	arg0.createQuery("from WorkPlanMain work left join fetch  work.workPlanExamines where work.id = ?");
						query.setParameter(0, jihua);
						return query.uniqueResult();
					}
					
				});
	}*/
	/**
	 * 根据类型和年份用户查询

	 * @param 工作日志
	 * @return
	 */
	public List selectWorkPlanMainYearAndTypeAndUser(final WorkPlanMain workPlanMain) {
		// TODO Auto-generated method stub

		return (List)this.getHibernateTemplate().execute(
				new HibernateCallback()
				{

					public Object doInHibernate(Session arg0)
							throws HibernateException, SQLException {
						// TODO Auto-generated method stub
						Query query =	arg0.createQuery("from  WorkPlanMain work where work.year = ? and work.type = ? and work.tjsUser.id = ? ");
						query.setParameter(0, workPlanMain.getYear());
						query.setParameter(1, workPlanMain.getType());	
						query.setParameter(2, workPlanMain.getTjsUser().getUserId());
						return query.list();
					}
					
				});
	}
	
	/**
	 * 查询默认日日志

	 * @param 工作日志
	 * @return
	 */
	public List selectDefanultDateWorkPlan(final WorkPlanMain workPlanMain) {
		// TODO Auto-generated method stub

		return (List)this.getHibernateTemplate().execute(
				new HibernateCallback()
				{

					public Object doInHibernate(Session arg0)
							throws HibernateException, SQLException {
						// TODO Auto-generated method stub
						Query query =	arg0.createQuery("from  WorkPlanMain work where work.year = ? and work.type = ? and work.tjsUser.id = ?");
						query.setParameter(0, workPlanMain.getYear());
						query.setParameter(1, workPlanMain.getType());	
						query.setParameter(2, workPlanMain.getTjsUser().getUserId());
						
						return query.list();
					}
					
				});
	}
	
	/**
	 * 根据类型和年份查询

	 * @param 工作日志
	 * @return
	 */
	public List selectWorkPlanMainYearAndType(final WorkPlanMain workPlanMain) {
		// TODO Auto-generated method stub

		return (List)this.getHibernateTemplate().execute(
				new HibernateCallback()
				{

					public Object doInHibernate(Session arg0)
							throws HibernateException, SQLException {
						// TODO Auto-generated method stub
						Query query =	arg0.createQuery("from  WorkPlanMain work where work.year = ? and work.type = ? ");
						query.setParameter(0, workPlanMain.getYear());
						query.setParameter(1, workPlanMain.getType());	
						return query.list();
					}
					
				});
	}

	/**
	 * 根据类型年份和状态查询

	 * @param 工作日志
	 * @return
	 */
	public List selectWorkPlanMainYearAndTypeAndStart(final WorkPlanMain workPlanMain) {
		// TODO Auto-generated method stub
		return (List)this.getHibernateTemplate().execute(
				new HibernateCallback()
				{

					public Object doInHibernate(Session arg0)
							throws HibernateException, SQLException {
						// TODO Auto-generated method stub
						Query query =	arg0.createQuery("from  WorkPlanMain work where work.year = ? and work.type = ? and work.start = ?");
						query.setParameter(0, workPlanMain.getYear());
						query.setParameter(1, workPlanMain.getType());
						query.setParameter(2, workPlanMain.getStart());	
						return query.list();
					}
					
				});

	}

	/**
	 * 根据计划ID查询同时抓取批示
	 * @param 工作日志
	 * @return
	 */
	public WorkPlanMain selectWorkPlan( String jihua) {
		WorkPlanMain workPlanMain = null;
		/*return (WorkPlanMain)this.getHibernateTemplate().execute(
				new HibernateCallback()
				{

					public Object doInHibernate(Session arg0)
							throws HibernateException, SQLException {
						// TODO Auto-generated method stub
						Query query =	arg0.createQuery("from  WorkPlanMain work inner join  fetch work.workPlanExamines where work.id = ?");
						query.setParameter(0, jihua);	
						return query.uniqueResult();
					}
					
				});*/
		Session session = getSession();
		Transaction transaction = 	session.beginTransaction();
		Query query =	session.createQuery("from  WorkPlanMain work left join  fetch work.workPlanExamines where work.id = ?");
		query.setParameter(0, jihua);
		workPlanMain = (WorkPlanMain) query.uniqueResult();
		transaction.commit();
		session.close();
		return workPlanMain;
	}

	public WorkPlanMain selectDateTime(final Date date, final String type, final Integer userid) {
		// TODO Auto-generated method stub
	
		return (WorkPlanMain)this.getHibernateTemplate().execute(
				new HibernateCallback()
				{

					public Object doInHibernate(Session arg0)
							throws HibernateException, SQLException {
						// TODO Auto-generated method stub
						Query query =	arg0.createQuery("from  WorkPlanMain work where work.time = ? and work.type = ? and work.tjsUser.id = ? ");
						query.setParameter(0, date);
						query.setParameter(1, type);
						query.setParameter(2, userid);
						return query.uniqueResult();
					}
					
				});
		
	}

	public List selectWorkPlanMainUserAndType(final WorkPlanMain workPlanMain) {
		// TODO Auto-generated method stub
		// TODO Auto-generated method stub
		return (List)this.getHibernateTemplate().execute(
				new HibernateCallback()
				{

					public Object doInHibernate(Session arg0)
							throws HibernateException, SQLException {
						// TODO Auto-generated method stub
						Query query =	arg0.createQuery("from  WorkPlanMain work where work.tjsUser.id = ? and work.type = ? ");
						query.setParameter(0, workPlanMain.getTjsUser().getUserId());
						query.setParameter(1, workPlanMain.getType());
						return query.list();
					}
					
				});
	}

	

	public List selectWorkPlanMainUserAndTypeAndStart(final WorkPlanMain workPlanMain) {
		// TODO Auto-generated method stub
		// TODO Auto-generated method stub
		return (List)this.getHibernateTemplate().execute(
				new HibernateCallback()
				{

					public Object doInHibernate(Session arg0)
							throws HibernateException, SQLException {
						// TODO Auto-generated method stub
						Query query =	arg0.createQuery("from  WorkPlanMain work where work.tjsUser.id = ? and work.type = ? ");
						query.setParameter(0, workPlanMain.getTjsUser().getUserId());
						query.setParameter(1, workPlanMain.getType());
						return query.list();
					}
					
				});
	}

	public List checkDefaultWorkPlanYear(final WorkPlanMain workPlanMain) {
		// TODO Auto-generated method stub
		// TODO Auto-generated method stub
		return (List)this.getHibernateTemplate().execute(
				new HibernateCallback()
				{

					public Object doInHibernate(Session arg0)
							throws HibernateException, SQLException {
						// TODO Auto-generated method stub
						Query query =	arg0.createQuery("from  WorkPlanMain work where work.tjsUser.id = ? and work.type = ? ");
						query.setParameter(0, workPlanMain.getTjsUser().getUserId());
						query.setParameter(1, workPlanMain.getType());
						// query.setParameter(2, workPlanMain.getSubmittype());
						return query.list();
					}
					
				});
	}
	public List selectWorkPlanMainUser(final WorkPlanMain workPlanMain) {
		// TODO Auto-generated method stub
		return (List)this.getHibernateTemplate().execute(
				new HibernateCallback()
				{

					public Object doInHibernate(Session arg0)
							throws HibernateException, SQLException {
						// TODO Auto-generated method stub
						Query query =	arg0.createQuery("from  WorkPlanMain work where work.tjsUser.id = ? ");
						query.setParameter(0, workPlanMain.getTjsUser().getUserId());
						
						return query.list();
					}
					
				});
	}	

	/**
	 * 工作日志模糊查询
	 */
	public List selectLikeWorkPlan(final WorkPlanMain workPlanMain) {
		// TODO Auto-generated method stub
		return (List)this.getHibernateTemplate().execute(
				new HibernateCallback()
				{

					public Object doInHibernate(Session arg0)
							throws HibernateException, SQLException {
						// TODO Auto-generated method stub
						Query query =	arg0.createQuery("from  WorkPlanMain work where work.tjsUser.id = ? and work.content like ?");
						query.setParameter(0, workPlanMain.getTjsUser().getUserId());
						query.setParameter(1, "%"+workPlanMain.getContent()+"%");
						return query.list();
					}
					
				});

	}

	/**
	 * 工作日志类型模糊查询
	 */
	public List selectLikeWorkPlanType(final WorkPlanMain workPlanMain) {
		// TODO Auto-generated method stub
		return (List)this.getHibernateTemplate().execute(
				new HibernateCallback()
				{

					public Object doInHibernate(Session arg0)
							throws HibernateException, SQLException {
						// TODO Auto-generated method stub
						Query query =	arg0.createQuery("from  WorkPlanMain work where work.tjsUser.id = ? and work.content like ? and work.type = ?");
						query.setParameter(0, workPlanMain.getTjsUser().getUserId());
						query.setParameter(1, "%"+workPlanMain.getContent()+"%");
						query.setParameter(2, workPlanMain.getType());
						return query.list();
					}
					
				});

	}		
	/**
	 * 根据用户和日期查询
	 * @param workPlanMain
	 * @return
	 */
	public WorkPlanMain selectWorkPlanDateDayUser(WorkPlanMain workPlanMain)
	{
		WorkPlanMain planMain = null;
		Session session = null;
		try {
			session = getSession();
			Transaction transaction =	session.beginTransaction();
			Query query =	session.createQuery("from WorkPlanMain work where work.time = ? and work.tjsUser.id = ?");
			query.setParameter(0, workPlanMain.getTime());
			query.setParameter(1, workPlanMain.getTjsUser().getUserId());
			List list =  query.list();
			if (list.size() > 0) {
				planMain = (WorkPlanMain)list.get(0);
			} else {
				planMain = null;
			}
			  
			transaction.commit();
		} catch (DataAccessResourceFailureException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally
		{
			session.close();
		}
		return planMain;
		
	}
	
}
